package jp.ac.u_tokyo.dch.service;

import java.util.List;

import jp.ac.u_tokyo.dch.core.db.DBConnection;
import jp.ac.u_tokyo.dch.core.exceptions.DBException;
import jp.ac.u_tokyo.dch.dao.user.UserGroupDao;
import jp.ac.u_tokyo.dch.dao.user.UserGroupInfoDao;
import jp.ac.u_tokyo.dch.dao.user.UsersDao;
import jp.ac.u_tokyo.dch.model.db.User;
import jp.ac.u_tokyo.dch.model.db.UserGroup;

public class UserService {

	/**
	* DBコネクションオブジェクト
	 */
	private DBConnection dbconn = null;


	/**
	 * オブジェクト生成者
	 * @param dbconn
	 */
	public UserService(DBConnection dbconn){
		this.dbconn = dbconn;
	}

	public List<User> searchUsers(String userid, String userName, String groupId, String groupName) throws DBException {

		UsersDao usersDao = new UsersDao(dbconn);
		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		List<User> list = usersDao.searchUsers(userid, userName, groupId, groupName);
		for (User user : list) {

			 user.setGroupNameList(userGroupInfoDao.selectGroupNameByUserId(user.getId()));
		}
		return list;
	}

	/**
	 * ユーザリストを取得する
	 * @return ユーザリスト
	 * @throws DBException 例外
	 */
	public List<User> selectUsers() throws DBException {

		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		UsersDao usersDao = new UsersDao(dbconn);

		List<User> list = usersDao.selectAll();

		for (User user : list) {

			 user.setGroupNameList(userGroupInfoDao.selectGroupNameByUserId(user.getId()));
		}

		return list;
	}

	/**
	 * ユーザグループ情報を取得する
	 * @param id
	 * @return ユーザグループ
	 * @throws DBException 例外
	 */
	public User selectUser(String id) throws DBException {

		UsersDao usersDao = new UsersDao(dbconn);
		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		User user = usersDao.selectById(id);
		user.setGroupList(userGroupInfoDao.selectGroupIdByUserId(user.getId()));
		return user;
	}

	/**
	 * ID重複チェック
	 * @param id グループID
	 * @return true : 存在する、false：存在しない
	 * @throws DBException 例外
	 */
	public boolean isExistsId(String id) throws DBException {

		UsersDao usersDao = new UsersDao(dbconn);
		return usersDao.checkId(id);
	}

	/**
	 *
	 * @return
	 * @throws DBException
	 */
	public List<UserGroup> selectUserGroup() throws DBException {

		UserGroupDao userGroupDao = new UserGroupDao(dbconn);

		return userGroupDao.selectAll();

	}

	/**
	 * 挿入する
	 * @param group グループ情報
	 * @return 挿入件数
	 * @throws DBException 例外
	 */
	public void insert(User user) throws DBException {
		UsersDao usersDao = new UsersDao(dbconn);
		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		usersDao.insert(user);
		userGroupInfoDao.insert(user.getId(), user.getGroupList());
	}

	/**
	 *
	 * @param group
	 * @return
	 * @throws DBException
	 */
	public void update(User user) throws DBException {
		UsersDao usersDao = new UsersDao(dbconn);
		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		usersDao.update(user);
		userGroupInfoDao.deleteByUserId(user.getId());
		userGroupInfoDao.insert(user.getId(), user.getGroupList());
	}

	/**
	 *
	 * @param group
	 * @return
	 * @throws DBException
	 */
	public void delete(User user) throws DBException {
		UsersDao usersDao = new UsersDao(dbconn);
		UserGroupInfoDao userGroupInfoDao = new UserGroupInfoDao(dbconn);
		usersDao.delete(user);
		userGroupInfoDao.deleteByUserId(user.getId());
	}
}
